<template>
	<el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" @keyup.enter="onLogin">
		<div class="login-title">{{ $t('app.registerIn') }}</div>
		<el-form-item prop="orgname">
			<el-input v-model="loginForm.name" :prefix-icon="House" :placeholder="$t('app.orgname')"></el-input>
		</el-form-item>
		<el-form-item prop="loginname">
			<el-input v-model="loginForm.loginname" :prefix-icon="House" :placeholder="$t('app.loginname')"></el-input>
		</el-form-item>
		<el-form-item prop="mobile">
			<el-input v-model="loginForm.mobile" :prefix-icon="User" :placeholder="$t('app.mobile')"></el-input>
		</el-form-item>
		<el-form-item prop="orgaddr">
			<el-input v-model="loginForm.addr" :prefix-icon="Setting" :placeholder="$t('app.orgaddr')"></el-input>
		</el-form-item>
		<el-form-item prop="pwd">
			<el-input v-model="loginForm.pwd" type="Password" :prefix-icon="Setting" :placeholder="$t('app.pwd')"></el-input>
		</el-form-item>
		<el-form-item prop="surepwd">
			<el-input v-model="loginForm.surepwd" type="Password" :prefix-icon="Setting" :placeholder="$t('app.surepwd')"></el-input>
		</el-form-item>
		<!-- <el-form-item prop="code" class="login-code">
			<el-input v-model="loginForm.code" :placeholder="$t('app.captcha')" :prefix-icon="Key"></el-input>
			<el-button v-if="!sms.disabled" type="default" @click="sendCode">发送验证码</el-button>
			<el-button v-else type="default" disabled>{{ sms.count }} 秒后重新发送</el-button>
		</el-form-item> -->
		<el-form-item class="login-button">
			<el-button type="primary" @click="onLogin()">{{ $t('app.registerIn') }}</el-button>
		</el-form-item>
	</el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Key,House,Setting } from '@element-plus/icons-vue'
import store from '@/store'
import {useOrgSubmitApi,getByCidApi } from '@/api/sys/orgs'
import {useUserSaveApi } from '@/api/sys/user'
import { useSendCodeApi } from '@/api/auth'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { validateMobile } from '@/utils/validate'
import { ElMessage } from 'element-plus'

// 短信计时器
const sms = reactive({
	disabled: false,
	total: 60,
	count: 0
})

const router = useRouter()
const { t } = useI18n()
const loginFormRef = ref()

const loginForm = reactive({
	name:'',
	addr:'',
	pid:'0',
	pwd:'',
	surepwd:'',
	mobile:'',
	loginname:''
})

const loginRules = ref({
	name: [{ required: true, message: t('required'), trigger: 'blur' }],
	addr: [{ required: true, message: t('required'), trigger: 'blur' }]
})

const onLogin = () => {
	loginFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		if(loginForm.pwd!=loginForm.surepwd){
			ElMessage.error('两次输入密码不一致！')
			return;
		}
		const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		if(!reg.test(loginForm.mobile)){
			ElMessage.error('输入手机号码不正确！')
			return;
		}
		// 机构注册
		useOrgSubmitApi(loginForm).then((response) => {
			//console.log(response);
			//router.push({ path: '/home' })
			let info:any=response;
			if(info.code==0){
				console.log(info);
				let data=info;
				let orgUId=data.id;
				var param={"cid":orgUId}
				getByCidApi(param).then((msg) => {
					let orgId=msg.data[0].id;
					var userparam={"username":loginForm.loginname
					,password:loginForm.surepwd,realName:"管理员"
					,gender:0,orgId:orgId,status:1,org:orgUId,
					mobile:loginForm.mobile,superAdmin:1,sort:1}
					useUserSaveApi(userparam).then((res) => {
						if(true){
							ElMessage.success({
								message: '操作成功',
								duration: 500
							})
							location.reload();
						}
					})
				})
			}
		})
	})
}
</script>

<style lang="scss" scoped>
.login-title {
	display: flex;
	justify-content: center;
	margin-bottom: 35px;
	font-size: 24px;
	color: #444;
	letter-spacing: 4px;
}
.login-code {
	:deep(.el-input) {
		width: 200px;
	}
	:deep(.el-button--default) {
		width: 150px;
		height: 45px;
		margin: 5px 0 0 10px;
	}
}
.login-button {
	:deep(.el-button--primary) {
		margin-top: 10px;
		width: 100%;
		height: 45px;
		font-size: 18px;
		letter-spacing: 8px;
	}
}
</style>
